<?PHP
/**
 * 一些工具类
 */
class Tool{
    /**
     * 获取阿里云通过SLB负载均衡取得客户端IP
     * @return String ip地址
     */
    public static function getForwardedForIp() {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
            $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $ip = trim($ip[0]);
            return $ip ? $ip : '127.0.0.1';
        }
        return Yii::app()->request->userHostAddress;
    }
    //通过秒数计算时间
    public static function getDataTime($time){
        $result = '';
        if($time < 60){
            return $time.'秒';
        }else if($time >= 60  && $time < 3600){
            $a = (int)($time/60);
            $b = $time - $a*60;
            return $a.'分'.$b.'秒';
        }else if($time >= 3600 && $time < 86400){
            $a = (int)($time/3600);//时
            $b = (int)(($time-(3600*$a))/60);//分
            $c = $time - 3600*$a - 60*$b;//秒
            return $a.'时'.$b.'分' . $c . '秒';
        }else if($time >= 86400){
            $a = (int)($time/86400);//天
            $b = (int)(($time-(86400*$a))/3600);//时
            $c = (int)(($time-(86400*$a + $b*3600))/60);//分
            $d = ($time-(86400*$a + $b*3600 + $c*60));//秒
            return $a.'天'.$b.'时' . $c . '分' . $d . '秒';
        }
    }	
}

?>